<?php
add_action('widgets_init', 'register_submit_coupon_widget'); // load widget

function register_submit_coupon_widget() { // register widget
    register_widget('submit_coupon');
}

class submit_coupon extends WP_Widget {// widget class {

    function submit_coupon() { 
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Submit coupon');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'submit_coupon');
        $this->WP_Widget('submit_coupon', 'CouponX - Submit coupon', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $post;
        $store_url = '';
        if($post->post_type == 'store'){
            $store_url = get_post_meta($post->ID,'url_store_metadata',true);
        }
        $title = apply_filters('widget_title', $instance['title']);
        $position = $instance['display_option'];
        ?>
        <script>
            jQuery(document).ready(function($) {
              $( "#ex_date" ).datepicker(
                  {dateFormat: 'yy/mm/dd'}
                );
            });
        </script>
        <div class="row-fluid submitcode">
            <div class="row-fluid submit_wg">
                <p class="submittitle"><img src="<?php echo get_stylesheet_directory_uri().'/img/kk.gif'?>" alt="submit a code" title="submit a code" /><?php echo $title;?></p>
            </div>
                    <div class="row-fluid from-add-cp">
                        <form id="coupon_submit" name="form_submit"  method="post" enctype="multipart/form-data">
                            <div class="row-fluid store_url_field">
                                <?php if($position =='home'){ ?>
                                <label>Store Name<span class="requir">*</span> :</label>
                               <?php }
                                     if($post && $post->post_type == 'store'){ ?>
                                        <input class="website" placeholder="e.g., storewebsite.com" 
                                               autocomplete="off" name="store_name" value="<?php echo get_post_meta($post->ID,'url_store_metadata',true);?>" readonly />
                                     <?php }else{?>
                                        <input class="website" placeholder="e.g., storewebsite.com" 
                                               autocomplete="off" name="store_name" />
                                     <?php }
                                  ?>
                                <div class="warming hiden domain-missing row-fluid">*Enter url store</div>
                                <div class="warming hiden domain-fail row-fluid">*Url store not match</div>
                                <div id="result" class="row-fluid">
                                    <div id="result_url_wrap"></div>
                                </div>
                            </div>
                            <div class="row-fluid title_field">
                                <?php if($position =='home'){ ?>
                                <label>Title Coupon<span class="requir">*</span> :</label>
                               <?php }?>
                                <input class="entertitle" type="text" placeholder="Enter Title"  autocomplete="off" name="title_coupon" value="<?php echo $_POST['title_coupon'];?>" />
                                <div class="warming hiden title-missing">*Enter title coupon</div>
                            </div>
                            <div class="row-fluid code_field">
                                <input class="entercode" type="text" placeholder="Enter Coupon code"  autocomplete="off" name="coupon_code" value="<?php echo $_POST['coupon_code'];?>" />
                                <div class="warming hiden code-missing">*Enter coupon code</div>
                            </div>
                            <div class="row-fluid des_field">
                                <?php if($position =='home'){ ?>
                                    <label>Describe the Discount<span class="requir">*</span> :</label>
                               <?php }?>
                                <textarea name="des_cp" placeholder="Description"></textarea>
                                <div class="warming hiden des-missing">*Enter description</div>
                            </div>
                            <div class="row-fluid exdate_field">
                                <label>Exporation Date (Optional) :</label>
                                <input class="date" type="text" placeholder="yy/mm/dd" autocomplete="off" name="ex_date" id="ex_date" value="<?php echo $_POST['ex_date'];?>"/>
                                <div class="warming hiden date-missing">*Enter Expiration Date</div>
                            </div>
                            <div class="row-fluid wrap-captcha security">
                                <img class="captcha_code" src="<?php echo get_template_directory_uri().'/inc/ajax-cp/cap/captcha_code_file.php'; ?>" alt="captcha"/>
                            </div>
                            <div class="row-fluid push_security">
                                <label>Please type the word above<span class="requir">*</span> :</label>
                                <input class="word" id="security_code" placeholder="Security Code" autocomplete="off" name="6_letters_code" type="text" />
                                <div class="warming hiden captcha-missing">*Enter Security code</div>
                                <div class="warming hiden captcha-fail">*Security code not match</div>
                            </div>
                            <div class="row-fluid submit_coupon">
                                <input id="submit_cp" type="submit" name="submit_coupon" value="Submit Code"/>
                            </div>
                        </form>
                    </div>
        </div>
        
   <?php }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['display_option'] = $new_instance['display_option'];
        return $instance;
    }

    function form($instance) {
        ?>
        <div style="color: #333;">
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'display_option' ); ?>"><?php _e('Position', ''); ?></label>
                <select name="<?php echo $this->get_field_name('display_option');?>" id="<?php echo $this-> get_field_id('display_option')?>" style="width: 60%;">
                    <option value='right' selected="true">Right Sidebar</option>
                    <option value='home'>Content Sidebar</option>
                </select>    
            </p>
        </div>
        <?php
    }
}
?>
